package com.chen.controller;

import com.chen.bean.Comment;
import com.chen.bean.Page;
import com.chen.bean.Result;
import com.chen.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Resource
    CommentService commentService;

    @PostMapping("/save")
    public Result saveComment(@RequestBody Comment comment){
        try {
            commentService.insertComment(comment);
            return Result.success(null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了",null);
        }
    }

    @GetMapping("/load")
    public Result loadCommentList(@RequestParam("title") String title,
                                  @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(name = "pageSize",defaultValue = "2") Integer pageSize){
        try {
            Page<List<Comment>> page = commentService.loadComment(title, pageNum, pageSize);
            return Result.success(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了",null);
        }
    }

    @GetMapping("/totalNum")
    public Result loadTotalComNum(String title){
        try {
            Integer total=commentService.getTotalNum(title);
            return Result.success(total);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(1,"出错了",null);
        }
    }
}
